<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/

class ClsLog
{
	private $path="";
        private $separator=',';
	function __construct()
	{
            $action=  ClsNaanalRequest::getInstance()->getAction();
            $page=ClsNaanalRequest::getInstance()->getModule();
            $switch=ClsNaanalRequest::getInstance()->getSwitch();
		$this->path="log/".$page."_".$action."_{$switch}.csv";
		$message='"Time","Depth","Message","File","Function","Args","Line","Previous File","Previous Function","Previous Args","Line"'."\n";
		file_put_contents($this->path, $message,FILE_APPEND); 		
	}
        function escapeCsvElement($str) 
        {
            return $str;
            $quot = false;
            $si = 0;
            $slen = strlen($str);
            $ret = '';
            while ($si < $slen) {
                $ch = $str[$si];
                if ($ch == $sep)
                    $quot = true;
                if ($ch == '"') {
                    $quot = true;
                    $ret .= '"';
                }      
                $ret .= $ch;
                $si++; 
            }      
            if ($quot)
                return '"' . $ret . '"';
            return $str;
        } 
	function log($message="None",$stepBack=0)
	{                          
		$time=date("m-d-Y h:i:s",time());
		$arr=debug_backtrace();
		$tab=count($arr)-2-$stepBack;	
		$message=$this->escapeCsvElement($message);
		$function=isset($arr[2+$stepBack]["function"])?$arr[2+$stepBack]["function"]:"";
		$previousFunction=isset($arr[2+$stepBack+1]["function"])?$arr[2+$stepBack+1]["function"]:"";
		
		if(isset($arr[2+$stepBack]["args"]))
		{
			$args="";
			foreach($arr[2+$stepBack]["args"] as $ind=>$arg)
			{
				if(is_object($arg))
				{
					$argScalar=get_class($arg)." object";
				}
				else if(is_array($arg))
				{
					$argScalar="Array";
				}
				else 
				{
					$argScalar=$arg;
				}
				if(empty($args))
				{
					$args="arg 0: ".$argScalar;
				}	
				else 
				{
					$args=$args." arg {$ind}: ".$argScalar;
				}			
			}
		}
		else 
			$args="";
                $args=$this->escapeCsvElement($args);
		if(isset($arr[2+$stepBack+1]["args"]))
		{
			$previousArgs="";
			foreach($arr[2+$stepBack+1]["args"] as $ind=>$arg)
			{
				if(is_object($arg))
				{
					$argScalar=get_class($arg)." object";
				}
				else if(is_array($arg))
				{
					$argScalar="Array";
				}
				else 
				{
					$argScalar=$arg;
				}
				if(empty($args))
				{
					$previousArgs="arg 0: ".$argScalar;
				}	
				else 
				{
					$previousArgs=$args." arg {$ind}: ".$argScalar;
				}			
			}
		}
		else 
			$previousArgs="";
                 $previousArgs=$this->escapeCsvElement($previousArgs);
		$line=isset($arr[1+$stepBack]["line"])?$arr[1+$stepBack]["line"]:"";
		$previousLine=isset($arr[1+$stepBack+1]["line"])?$arr[1+$stepBack+1]["line"]:"";
		$file=isset($arr[1+$stepBack]["file"])?$arr[1+$stepBack]["file"]:"";
		$previousFile=isset($arr[1+$stepBack+1]["file"])?$arr[1+$stepBack+1]["file"]:"";
		$message=$time.','.$tab.',"'.$message.'","'.$file.'","'.$function.'","'.$args.'",'.$line.',"'.$previousFile.'","'.$previousFunction.'","'.$previousArgs.'",'.$previousLine."\n";
		$fh = fopen($this->path, 'a') or trace("can't open file");
		fwrite($fh, $message);
		fclose($fh);
	} 
}
?>